    const dotenv = require("dotenv")
    dotenv.config()

    //把静态文件上传到腾讯云cos
    const Cos = require("cos-nodejs-sdk-v5");

    const fs = require("fs");
    const path = require("path");
    
    const os = require("os");
    
    const PUBLIC_PATH = path.join(__dirname, "/");
    
    const client = new Cos({
        SecretId: process.env.SecretId,
        SecretKey: process.env.SecretKey
    });
    
    async function deleteDir(dir) {
  
    let result = await client.getBucket({
        Bucket: process.env.Bucket, /* 必须 */
        Region: process.env.Region,  
        Prefix: dir + "/",
    },function (err,result){
       if (result.Contents) {
            let keys = [];
            result.Contents.forEach(function(obj) {
                keys.push({"Key":obj.Key});
            });
          
            try {
              client.deleteMultipleObject( {
                Bucket: process.env.Bucket, /* 必须 */
                Region: process.env.Region,          
                Objects: keys
              });
              console.log("删除成功");
            } catch (e) {
              console.log("文件删除失败", e);
            }
          }
      
    });

    }
    
    

    function addFileToCosSync(src, dist) {
    let docs = fs.readdirSync(src);
    docs.forEach(function(doc) {
      let _src = src + "/" + doc,
        _dist = dist + "/" + doc;
      let st = fs.statSync(_src);
      // 判断是否为文件  
        if (st.isFile() && doc !== ".DS_Store") {
        putCos(_src, _dist);
      }
      // 如果是目录则递归调用自身 
         else if (st.isDirectory()) {
        addFileToCosSync(_src, _dist);
      }
    });}
    
    /** *单个文件上传至cos */
    async function putCos(src, dist) {
       console.log(src)
        await client.putObject({
            Bucket: process.env.Bucket, /* 必须 */
            Region: process.env.Region,      
            Key: dist,              /* 必须 */
            Body: fs.createReadStream(src),
        },function(err,data){
            if( err != null)
            console.log("上传失败".err);
        })
        
    } 
    
    /** *上传文件启动 *@param {string} dirName 将要上传的文件名 */
    async function upFile(dirName) {
    try {
      await deleteDir(dirName);
      await addFileToCosSync(PUBLIC_PATH + ".nuxt/dist/client", dirName);
      console.log(dirName + "上传cos成功");
    } catch (err) {
      console.log(dirName + "上传cos成功失败", err);
    }}upFile(process.env.UploadPath);